package models

import (
	"github.com/astaxie/beego/orm"
	"fmt"
)

type Msg struct {
	Code int
	Data interface{}
}


type User struct {
	Id int
	Name string `orm:"size(100)"`
}

func init()  {
	fmt.Println("uuuuuuuuuuuser init")
	orm.RegisterModel(new(User))
	//read()
}

func QueryOne(id int) (User, error) {
	u := User{Id: id}
	err := orm.NewOrm().Read(&u)
	return u, err
}

func QuerList() ([]User, int64, error) {
	var users []User
	num, err := orm.NewOrm().QueryTable("user").All(&users)
	//num, err := orm.NewOrm().QueryTable("user").Filter("name", "111").All(&users)
	fmt.Println("query lsit ------- ", num, err)
	return users, num, err
}

func Insert(user User) (int64, error) {
	return orm.NewOrm().Insert(&user)
}

func Delete(id int) (int64, error) {
	o := orm.NewOrm()
	num, err := o.Delete(&User{Id: id})
	return num, err
}

func Update(user User) (int64, error) {
	o := orm.NewOrm()
	num, err := o.Update(&user)
	return num, err
}